﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pub;
using System.Data;

namespace JTBox
{
    public partial class _Default : System.Web.UI.Page
    {
        string cmd = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            CDebug.Send(Request.Url.ToString());
            cmd = Request["cmd"] != null ? Request["cmd"] : "";

            switch (cmd)
            {
                case "login":
                    chkLogin();
                    break;
                case "islogin":
                    isLogin();
                    break;
                case "logout":
                    logout();
                    break;
            }
        }

        protected void chkLogin()
        {
            string sUser = Request["user"] != null ? Request["user"] : "";
            string sPass = Request["pass"] != null ? Request["pass"] : "";
            try
            {
                string strSQL = "SELECT * FROM WP_USER WHERE [User]='" + sUser + "' AND md5='" + CPub.getMD5(sPass) + "'";
                CADO ado1 = new CADO();
                DataSet ds1 = ado1.sel1(strSQL);
                CDebug.Send(strSQL + "\n--返回结果个数：" + ado1.ds_count);
                if (!CDB.dsIsNull(ds1))
                {
                    Session["user_id"] = ds1.Tables[0].Rows[0]["u_ID"].ToString();
                    Session["user"] = sUser;
                    string s1 = "{'sta':1,'txt':'登录成功！'}";
                    s1 = s1.Replace("'", "\"");
                    Response.Write(s1);
                }
                else
                {
                    Session["user_id"] = null;
                    Session["user"] = null;
                    string s1 = "{'sta':0,'txt':'用户名或密码不正确'}";
                    s1 = s1.Replace("'", "\"");
                    Response.Write(s1);
                }
            }
            catch (Exception ex)
            {
                string s1 = "{'sta':0,'txt':'用户名或密码不正确'}";
                s1 = s1.Replace("'", "\"");
                Response.Write(s1);
                CDebug.SendError(ex.ToString());
            }
            
            Response.End();
        }

        protected void isLogin()
        {
            string s1 = "";
            if (Session["user"] != null)
            {
                s1 = "{'sta':1,'user':'" + Session["user"] + "'}";
            }
            else
            {
                s1 = "{'sta':0}";
            }
            Response.Write(s1);
            Response.End();
        }

        protected void logout()
        {
            Session["user"] = null;
            Session["user_id"] = null;
        }
    }
}